home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Applications / MathPad 2.4 / XFuns / XFun kit / scatter src / scatter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-09  |  2.4 KB  |  112 lines  |  [TEXT/CWIE]

  1. /*
  2. The XFun scatter() is meant more as an example than an actual useful function.
  3. It opens a window and displays all the XY points passed to it during one evaluation.
  4. The XY coordinates passed are used directly as window pixel coordinates.
  5. */
  6.  
  7. #include "callbackg.h"
  8. #include "XFundef.h"
  9.  
  10. /****** globals ****/
  11.  
  12. WindowPtr myWindow;
  13. CursHandle myCursor;
  14.  
  15. long npts;
  16. struct coord **ploth;
  17.  
  18. /* called once from main() when MathPad starts up */
  19. void initialize(void)
  20. {    
  21.     myWindow = GetNewWindow(128,0L,0L);    /* XFun resource file is open now. Closes on return */
  22.     SetWRefCon(myWindow,0L);            /* !!! MathPad requires 0 RefCon for XFun windows */
  23.     SetWTitle(myWindow,"\pSCATTER PLOT");
  24.     ploth = (struct coord **)NewHandle(0L);
  25.     npts = 0;
  26.     myCursor = GetCursor(plusCursor);    /* test display of a different cursor */
  27.     HLock((Handle)myCursor);
  28. }
  29.  
  30. /* called before each evaluation of the document */
  31. void dopredef(void)
  32. {
  33.    GrafPtr oldPort;
  34.    
  35.    if(npts)
  36.    {
  37.     SetHandleSize((Handle)ploth,0L);    // deallocate old plot data
  38.     npts = 0;
  39.     GetPort(&oldPort);
  40.     SetPort(myWindow);
  41.     InvalRect(&myWindow->portRect);
  42.     SetPort(oldPort);
  43.    }
  44.    else HideWindow(myWindow);
  45. }
  46.  
  47. static void drawpoint(float x,float y)
  48. {
  49.    short ix,iy;
  50.    ix = x;            // some scaling here might be nice
  51.    iy = y;
  52.    MoveTo(ix,iy);
  53.    Line(0,0);
  54. }
  55.  
  56. /* called each time scatter() is used. */
  57. int dofuncall(double *retval)
  58. {
  59.    struct coord *pt;
  60.    double *a;
  61.    int ok;
  62.    long rows,cols;
  63.    EXPR xpr;
  64.    GrafPtr oldPort;
  65.  
  66.    MakeParmExpr(0,&xpr);
  67.    ok = GetExprMatrix(xpr,&a,&rows,&cols);
  68.    FreeExpr(xpr);
  69.    if(!ok || cols !=0 || rows != 2)
  70.    {
  71.     ErrMsg(" expected {x,y}",0L);
  72.     DisposPtr((Ptr)a);
  73.     return(FALSE);
  74.    }
  75.    npts++;
  76.    SetHandleSize((Handle)ploth,npts*sizeof(struct coord));        // alloc space for this point
  77.    if(MemError())
  78.    {
  79.     ErrMsg(" scatter() out of memory %ld points",(char *)npts);
  80.     DisposPtr((Ptr)a);
  81.     return(FALSE);
  82.    }
  83.    pt = *ploth + npts-1;
  84.    pt->x = a[0];
  85.    pt->y = a[1];            // save the point for window updates
  86.    DisposPtr((Ptr)a);
  87.    ShowWindow(myWindow);
  88.    GetPort(&oldPort);
  89.    SetPort(myWindow);
  90.    drawpoint(pt->x,pt->y);
  91.    SetPort(oldPort);
  92.    *retval = npts;
  93.    return(TRUE);
  94. }
  95.  
  96. /* called on window update. Grafport is already set */
  97. void DrawPlot()
  98. {
  99.    struct coord *pt;
  100.    long n;
  101.    
  102.    HLock((Handle)ploth);
  103.    pt = *ploth;
  104.    n = npts;
  105.    while(n--)
  106.    {
  107.     drawpoint(pt->x,pt->y);
  108.     pt++;
  109.    }
  110.    HUnlock((Handle)ploth);
  111. }
  112.